import tkinter as tk
from tkinter import filedialog, messagebox
import meigong  # 导入 pic_operate 函数
import os

class FileProcessorApp:
    def __init__(self, root):
        self.root = root
        self.root.title("文件处理工具")
        self.root.geometry("500x400")
        
        # 创建一个标签和按钮，用户选择文件夹
        self.folder_path_label = tk.Label(root, text="选择文件夹：", font=("Arial", 12))
        self.folder_path_label.pack(pady=10)
        
        self.folder_path_button = tk.Button(root, text="浏览", font=("Arial", 12), command=self.choose_directory)
        self.folder_path_button.pack(pady=5)
        
        # 显示文件夹路径
        self.path_display = tk.Label(root, text="", font=("Arial", 10), fg="blue")
        self.path_display.pack(pady=10)
        
        # 显示文件列表
        self.files_label = tk.Label(root, text="文件列表：", font=("Arial", 12))
        self.files_label.pack(pady=10)
        
        self.files_listbox = tk.Listbox(root, height=10, width=50, font=("Arial", 10))
        self.files_listbox.pack(pady=10)
        
        # 执行操作按钮
        self.process_button = tk.Button(root, text="执行操作", font=("Arial", 12), command=self.perform_operations)
        self.process_button.pack(pady=10)

    def choose_directory(self):
        """让用户选择文件夹"""
        folder_path = filedialog.askdirectory(title="选择文件夹")
        if folder_path:
            self.folder_path = folder_path
            self.path_display.config(text=f"选择的文件夹：{folder_path}")
            self.update_file_list()
        else:
            messagebox.showwarning("警告", "没有选择文件夹！")

    def update_file_list(self):
        """更新文件列表"""
        self.files_listbox.delete(0, tk.END)  # 清空现有的文件列表
        if hasattr(self, 'folder_path') and self.folder_path:
            try:
                files = os.listdir(self.folder_path)
                for file in files:
                    file_path = os.path.join(self.folder_path, file)
                    if os.path.isfile(file_path):
                        self.files_listbox.insert(tk.END, file)
            except Exception as e:
                messagebox.showerror("错误", f"无法读取文件夹内容: {str(e)}")

    def perform_operations(self):
        """对文件夹中的文件执行操作"""
        if not hasattr(self, 'folder_path') or not self.folder_path:
            messagebox.showwarning("警告", "请先选择文件夹！")
            return
        
        # 调用从模块导入的 pic_operate 函数
        meigong.pic_operate(self.folder_path)

# 创建并运行应用
root = tk.Tk()
app = FileProcessorApp(root)
root.mainloop()
